package com.service.impl;

import com.dto.OutGoodsDTO;
import com.dto.UnloadGoodsDTO;
import com.entity.Goods;
import com.entity.GoodsEntity;
import com.entity.OutGoods;
import com.entity.PageBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.GoodsEntityMapper;
import com.mapper.GoodsMapper;
import com.mapper.OutGoodsMapper;
import com.service.OutGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: xuan
 * @Date: 2024/7/5 11:01
 * @Version: 1.0
 */
@Slf4j
@Service
public class OutGoodsServiceImpl implements OutGoodsService {
    @Autowired
    GoodsEntityMapper goodsEntityMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    OutGoodsMapper outGoodsMapper;
    @Override
    @Transactional
    public void outGoods(OutGoods outGoods) {
//        给定时间，出库
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        outGoods.setOutTime(sdf.format(date));
        //查询商品和商品数余和当前出库值的差。
        Integer currentSum = 0;
        GoodsEntity goodsEntity = null;
        try {
            goodsEntity = goodsEntityMapper.findByGoodsUniqueNo(outGoods.getGoodsUniqueNo());
            currentSum = goodsEntity.getSum() - outGoods.getOutNumber();//商品余额-出库
            if (currentSum < 0) {
                throw new RuntimeException("商品库余不足,当前库存：" + goodsEntity.getSum());
            }
        } catch (RuntimeException e) {
            throw new RuntimeException(e.getMessage());
        } catch (Exception e) {
            throw new RuntimeException("数据格式有误");
        }
        //先修改总额
        goodsEntity.setSum(currentSum);
        //此处modify查询的是用名字和货物唯一编码来修改。
        goodsEntityMapper.modify(goodsEntity);
        //再查货物批次列表。
        List<Goods> goodsList = goodsMapper.findByGoodsUniqueNo(goodsEntity.getGoodsUniqueNo());
        //日期排序
        //安装日期顺序排序。
        goodsList.sort((t1, t2) -> t1.getProductProductionDate().compareTo(t2.getProductProductionDate()));
        //按照指定生产日期早的先出货原理
        Integer outNumber = outGoods.getOutNumber(); //临时出库数,配合循环进行批次出库
        for (Goods goods : goodsList) {
            if (outNumber < goods.getNumber()) {
                //出库数（临时数）小于此批次货物数额
                goods.setNumber(goods.getNumber() - outNumber);
                //进行商品批次货物修改
                goodsMapper.modify(goods);
                //商品总额修改
                goodsEntity.setSum(goodsEntity.getSum() - outGoods.getOutNumber());
                goodsEntityMapper.modify(goodsEntity);
                //出库完毕，添加出库记录。
                outGoods.setGoodsSum(goodsEntity.getSum());
                outGoodsMapper.insert(outGoods);
                return;
            } else if (outNumber == goods.getNumber()) {
                //出库数(临时数)等于此批次货物余额
                goods.setNumber(goods.getNumber() - outGoods.getOutNumber());
                //商品批次数量为0，删除商品批次。
                goodsMapper.isDelete(goods);
                //商品总额修改
                goodsEntity.setSum(goodsEntity.getSum() - outGoods.getOutNumber());
                goodsEntityMapper.modify(goodsEntity);
                //出库完毕，添加出库记录。
                outGoods.setGoodsSum(goodsEntity.getSum());
                outGoodsMapper.insert(outGoods);
                return;
            } else if (outNumber > goods.getNumber()) {
                //出库数（临时数）大于当前批次数额
                outNumber = outNumber - goods.getNumber();
                //出完当前批次货物
                goods.setNumber(0);
                goodsMapper.isDelete(goods);
            }
        }
    }

    @Override
    public PageBean<OutGoodsDTO> findAll(OutGoods outGoods, Integer pageIndex, Integer pageSize) {
        //分页查询
        PageHelper.startPage(pageIndex, pageSize);
        List<OutGoodsDTO> all = outGoodsMapper.findAll(outGoods);
        PageInfo<OutGoodsDTO> pageInfo = new PageInfo<>(all);
        PageBean<OutGoodsDTO> pageBean = new PageBean<>(
                pageIndex,
                pageSize,
                pageInfo.getPages(),
                (int)pageInfo.getTotal(),
                pageInfo.getList()
        );
        log.info("分页查询成功,值为:{}",pageBean.getBeanList());
        return pageBean;
    }

}


